package com.qd.qx.widget;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * Code is far away from bug with the animal protecting
 * Author：ys on 2018/7/17
 * Function：viewpager fragment adapter
 */
public class BaseFragmentPagerAdapter extends FragmentPagerAdapter {
    private FragmentManager mfragmentManager;
    private List<Fragment> mlist;
    private List<String> tabTitle;//带tab头的展示集合

    public BaseFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.mlist = list;
    }

    public BaseFragmentPagerAdapter(FragmentManager fm, List<Fragment> list, List<String> tabTitle) {
        super(fm);
        this.mlist = list;
        this.tabTitle = tabTitle;
    }

    @Override
    public Fragment getItem(int arg0) {
        return mlist.get(arg0);//显示第几个页面
    }

    @Override
    public int getCount() {
        return mlist.size();//有几个页面
    }

    @Override
    public CharSequence getPageTitle(int position) {
        //如果有tab头的返回显示数据
        if (null != tabTitle) {
            return tabTitle.get(position % tabTitle.size());
        }
        return super.getPageTitle(position);
    }

    //    myViewPager.setOnPageChangeListener(new MyPagerChangeListener());
//
////把Fragment添加到List集合里面
//    list = new ArrayList<>();
//list.add(new OneFragment());
//list.add(new TwoFragment());
//list.add(new ThreeFragment());
//    adapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), list);
//myViewPager.setAdapter(adapter);
//myViewPager.setCurrentItem(0);  //初始化显示第一个页面
//tv_item_one.setBackgroundColor(Color.RED);//被选中就为红色


//    /**
//     * 设置一个ViewPager的侦听事件，当左右滑动ViewPager时菜单栏被选中状态跟着改变
//     *
//     */
//    public class MyPagerChangeListener implements OnPageChangeListener {
//
//        @Override
//        public void onPageScrollStateChanged(int arg0) {
//        }
//
//        @Override
//        public void onPageScrolled(int arg0, float arg1, int arg2) {
//        }
//
//        @Override
//        public void onPageSelected(int arg0) {
//          switch (arg0) {
//                case 0:
//                    viewPager.setCurrentItem(0);
//                    ToastUitl.showShort("第一个");
//                    break;
//                case 1:
//                    viewPager.setCurrentItem(1);
//                    ToastUitl.showShort("第二个");
//                    break;
//                case 2:
//                    viewPager.setCurrentItem(2);
//                    ToastUitl.showShort("第三个");
//                    break;
//            }
//        }
//    }

}
